import Vue from 'vue'
import Router from 'vue-router'
import store from "./../store/index"
Vue.use(Router)
const Routes = [
  { path: '*', redirect: '/index' },
  {
    path: '/index',
    name: 'index',
    component: () => import('@/pages/home/index')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/pages/home/login')
  },
  {
    path: '/vueBus',
    name: 'vueBus',
    component: () => import('@/pages/learn/vueBus')
  },
  // 用户管理
  {
    path: '/userManage',
    name: 'userManage',
    component:() => import('@/pages/permissionManage/userManage')
  },
  // 菜单管理
  {
    path: '/menuManage',
    name: 'menuManage',
    component:() => import('@/pages/permissionManage/menuManage')
  },
  // 角色管理
  {
    path: '/roleManage',
    name: 'roleManage',
    component:() => import('@/pages/permissionManage/roleManage')
  },
  // 组织管理
  {
    path: '/orgManage',
    name: 'orgManage',
    component:() => import('@/pages/permissionManage/orgManage')
  },
  // 核酸检测点管理
  {
    path: '/hsjcdManage',
    name: 'hsjcdManage',
    component:() => import('@/pages/dataMange/hsjcdManage')
  },
  // 疫苗接种点管理
  {
    path: '/ymjzdManage',
    name: 'ymjzdManage',
    component:() => import('@/pages/dataMange/ymjzdManage')
  },
  // 核酸监测结果查看
  {
    path: '/hesuanData',
    name: 'hesuanData',
    component:() => import('@/pages/dataView/hesuanData')
  },
  // 疫苗接种记录查看
  {
    path: '/yimiaoData',
    name: 'yimiaoData',
    component:() => import('@/pages/dataView/yimiaoData')
  },
  // 阳性数据统计
  {
    path: '/yangData',
    name: 'yangData',
    component:() => import('@/pages/dataView/yangData')
  },
]
const createRouter = () => new Router({
  mode: "hash",	// 或者”hash”
  base: __dirname,		// 或者是加入上下文路径
  routes: Routes,
})
const router = createRouter()
// 注册全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.name == "login") {// 如果跳到登录页
    next();
  } else {// 如果跳到其他页面
    if (document.cookie && document.cookie.indexOf("token=") > -1) {// cookie存在
      next();
    } else {// cookie不存在，说明是未登录或者登录到期，需要重新登录
      next({
        name: 'login',
        params: {
          targetPage: to.name
        }
      })
    }
  }
})

export default router;
